<?php

namespace app\model;

//use support\Model;
use support\Db;
use Illuminate\Database\Eloquent\Model;

class WaOrdersRelevance
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'wa_orders_sales_relevance';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    public static function allByMoreLike($con_one,$con_two,$con_three,$con_four)
    {
        $list = Db::table('wa_orders_sales_relevance')
            ->select('*')
            ->where($con_one)
            ->orWhere($con_two)
            ->orWhere($con_three)
            ->orWhere($con_four)
            ->get();

        if ($list){
            foreach ($list as $k){


                if ($k->price_options){
                    $k->price_options = json_decode($k->price_options);
                }

                if ($k->option_package){
                    $k->option_package = json_decode($k->option_package);
                }
//
                if ($k->other_price){
                    $k->other_price = json_decode($k->other_price);
                }



                // car_type  1 轿车 2 SUV 3 MPV 4 跑车 5 新能源
                if ($k->car_type == 1){
                    $k->car_type_name = '轿车';
                }elseif($k->car_type == 2){
                    $k->car_type_name = 'SUV';
                }elseif($k->car_type == 3){
                    $k->car_type_name = 'MPV';
                }elseif($k->car_type == 4){
                    $k->car_type_name = '跑车';
                }elseif($k->car_type == 5){
                    $k->car_type_name = '新能源';
                }else{
                    $k->car_type_name = '';
                }


                // 车辆状况(是否是新车)  1 新车 2 二手车
                if ($k->vehicle_status == 1){
                    $k->vehicle_status_name = '新车';
                }elseif ($k->vehicle_status == 2){
                    $k->vehicle_status_name = '二手车';
                }else{
                    $k->vehicle_status_name = '';
                }



                // 是否新能源 1 是 2 不是
                if($k->is_nev == 1){
                    $k->is_nev_name = '新能源';
                }elseif ($k->is_nev == 2){
                    $k->is_nev_name = '传统能源';
                }else{
                    $k->is_nev_name = '';
                }
                $vins = WaOrdersOperateLogVin::all(['order_sn_parent' => $k->order_sn_child]);
                if ($vins){
                    $vins = json_decode($vins);
                }else{
                    $vins = [];
                }
                $k->vins = $vins;
            }
        }
        return json_encode($list);
    }


    public static function all($condition)
    {
        $list = Db::table('wa_orders_sales_relevance')
            ->select('*')
            ->where($condition)
            ->get();
        if ($list){
            foreach ($list as $k){

                if ($k->price_options){
                    $k->price_options = json_decode($k->price_options);
                }

                if ($k->option_package){
                    $k->option_package = json_decode($k->option_package);
                }

                if ($k->other_price){
                    $k->other_price = json_decode($k->other_price);
                }

                if ($k->vin){
                    $k->vin = json_decode($k->vin);
                }

                // car_type  1 轿车 2 SUV 3 MPV 4 跑车 5 新能源
                if ($k->car_type == 1){
                    $k->car_type_name = '轿车';
                }elseif($k->car_type == 2){
                    $k->car_type_name = 'SUV';
                }elseif($k->car_type == 3){
                    $k->car_type_name = 'MPV';
                }elseif($k->car_type == 4){
                    $k->car_type_name = '跑车';
                }elseif($k->car_type == 5){
                    $k->car_type_name = '新能源';
                }else{
                    $k->car_type_name = '';
                }


                // 车辆状况(是否是新车)  1 新车 2 二手车
                if ($k->vehicle_status == 1){
                    $k->vehicle_status_name = '新车';
                }elseif ($k->vehicle_status == 2){
                    $k->vehicle_status_name = '二手车';
                }else{
                    $k->vehicle_status_name = '';
                }



                // 是否新能源 1 是 2 不是
                if($k->is_nev == 1){
                    $k->is_nev_name = '新能源';
                }elseif ($k->is_nev == 2){
                    $k->is_nev_name = '传统能源';
                }else{
                    $k->is_nev_name = '';
                }
                $vins = WaOrdersOperateLogVin::all(['order_sn_parent' => $k->order_sn_child]);
                if ($vins){
                    $vins = json_decode($vins);
                }else{
                    $vins = [];
                }
                $k->vins = $vins;
            }
        }
        return json_encode($list);
    }

    public static function find($condition)
    {
        $res = Db::table('wa_orders_sales_relevance')
            ->select('*')
            ->where($condition)
            ->first();
        return json_encode($res);
    }



    public static function childrenSupplierDistinct($childrenFile,$condition)
    {

        $list = Db::table('wa_orders_sales_relevance')
            ->select($childrenFile)
            ->where($condition)
            ->distinct()
            ->get();
        return json_encode($list);

    }
    public static function childrenCount($condition)
    {

        $count = Db::table('wa_orders_sales_relevance')
            ->where($condition)
            ->count();
        return $count;

    }

    public static function created($data){
        $result = Db::table('wa_orders_sales_relevance')->insertGetId($data);
        return $result;
    }

    public static function updated($data,$where){
        $result = Db::table('wa_orders_sales_relevance')->where($where)->update($data);
        return $result;
    }

    public static function geted($file,$where){
        $result = Db::table('wa_orders_sales_relevance')->select($file)->where($where)
            ->get();
        return json_decode($result);
    }

    public static function delete($where){
        $result = Db::table('wa_orders_sales_relevance')->where($where)->delete();
        return $result;
    }

    public static function sums($where){
        $result = Db::table('wa_orders_sales_relevance')->where($where)->sum('total_price');
        return $result;
    }

    public static function counts($where){
        $result = Db::table('wa_orders_sales_relevance')->where($where)->sum('count');
        return $result;
    }

    public static function decrement($where,$count){
        $result = Db::table('wa_orders_sales_relevance')
            ->where($where)
            ->decrement('to_be_purchase_count', $count);
        return $result;
    }


}